package Server.TechnicalServices.Mapper;

import java.util.ArrayList;

import EntityCondivise.ETaskStato;
import Server.Entity.Dominio.EDipendenza;
import Server.Entity.Dominio.ETask;
import Server.Entity.Dominio.ETimeRecord;
import Server.Entity.IceClasses.EDipendenzaI;
import Server.Entity.IceClasses.ETaskI;

public class MTask implements Mapper{

	@Override
	public ETaskI getIceObj(Object hibObj) {

		ETaskI iceTask = null;
		MMoney moneyMapper = new MMoney();
		MTimeRecord trMapper = new MTimeRecord();
		MDipendenza dipMapper = new MDipendenza();
		MConsulente consulenteMapper = new MConsulente();		
		MTaskStato statoMapper = new MTaskStato();
		
		if ( hibObj instanceof ETask)
		{
			iceTask = new ETaskI();
			iceTask.statoObj = new ETaskStato();
			iceTask.timerecords = new ArrayList<EntityCondivise.ETimeRecord>();
			iceTask.dipendenze = new ArrayList<EntityCondivise.EDipendenza>();
			iceTask.statoObj.task = new ETaskI();
			
			iceTask.id = ((ETask) hibObj).getId();
			iceTask.titolo = ((ETask) hibObj).getTitolo();
			
			iceTask.consulenteAttuale = consulenteMapper.getIceObj( ((ETask) hibObj).getConsulenteAttuale() );
			
			iceTask.statoObj = statoMapper.getIceObj( ((ETask) hibObj).getStatoObj());
			iceTask.statoObj.task = iceTask;
			
			System.out.println("maper task"+iceTask.id);
			
			System.out.println("dipendenze 0 ="+((ETask) hibObj).getDipendenze().size());
			
			for(EDipendenza hibDip: ( (ETask) hibObj).getDipendenze() )
			{
				System.out.println("dipendenze 1 ="+((ETask) hibObj).getDipendenze().size());
				EDipendenzaI iceDip = dipMapper.getIceObj(hibDip);
				iceDip.figlio = iceTask;
				iceTask.dipendenze.add( iceDip );
				System.out.println("dipendenze 2 ="+((ETask) hibObj).getDipendenze().size());
				
			}
			
			for(ETimeRecord hibTr: ((ETask) hibObj).getTimerecords())
				iceTask.timerecords.add( trMapper.getIceObj(hibTr) );
			
			iceTask.budget = moneyMapper.getIceObj( ((ETask) hibObj).getBudget() );
			iceTask.costo = moneyMapper.getIceObj( ((ETask) hibObj).getCosto() );
			iceTask.costoOrario = moneyMapper.getIceObj( ((ETask) hibObj).getCostoOrario() );
			
			iceTask.fine = ((ETask) hibObj).getFine();
			iceTask.inizio = ((ETask) hibObj).getInizio();
		}
		
		return iceTask;
	}

	@Override
	public ETask getHibObj(Object iceObj) {

		ETask hibTask = null;
		MMoney moneyMapper = new MMoney();
		MTimeRecord trMapper = new MTimeRecord();
		MDipendenza dipMapper = new MDipendenza();
		MConsulente consulenteMapper = new MConsulente();
		MTaskStato statoMapper = new MTaskStato();		
		
		if (iceObj instanceof ETaskI)
		{
			hibTask = new ETask();
			
			hibTask.setId( ((ETaskI) iceObj).id );
			hibTask.setTitolo( ((ETaskI) iceObj).titolo );
			
			hibTask.setConsulenteAttuale( consulenteMapper.getHibObj( ((ETaskI) iceObj).consulenteAttuale) );
			
			hibTask.setStatoObj( statoMapper.getHibObj( ((ETaskI) iceObj).statoObj) );
			hibTask.getStatoObj().setTask(hibTask);
			
			for(EntityCondivise.EDipendenza iceDip: ((ETaskI) iceObj).dipendenze)
			{
				EDipendenza dipendenza = new EDipendenza();
				dipendenza = dipMapper.getHibObj(iceDip);
				dipendenza.setFiglio( hibTask );
				hibTask.getDipendenze().add( dipendenza );
			}
			
			for(EntityCondivise.ETimeRecord iceTr: ((ETaskI) iceObj).timerecords)
				hibTask.getTimerecords().add( trMapper.getHibObj(iceTr) );			
			
			hibTask.setBudget( moneyMapper.getHibObj( ((ETaskI) iceObj).budget) );
			hibTask.setCosto( moneyMapper.getHibObj( ((ETaskI) iceObj).costo) );
			hibTask.setCostoOrario( moneyMapper.getHibObj( ((ETaskI) iceObj).costoOrario) );
			
			hibTask.setFine( ((ETaskI) iceObj).fine );
			hibTask.setInizio( ((ETaskI) iceObj).inizio );
		}
		
		return hibTask;
		
	}
	
}
